//Lindsay Ergenekan 
//ergenekl@onid.oregonstate.edu
//CS311-400
//Homework3 
//


#include <stdio.h> 
#include <getopt.h> 
#include <time.h> 
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/types.h>


#include <fcntl.h> 

 int demo(); 

int main(int argc, char **argv)
{ 
char c; 
struct stat s; 
int lflag=0;
int off1flag = 0; 
int off2flag = 0;
int fflag = 0;
int err = 0;
int length = 0;
int offset1 = 0;
int offset2 = 0;
char *file_name;
int inputFd, outputFd;
int numRead; 

while ((c = getopt(argc, argv, "f:o:O:l:v")) != -1)
    switch (c){ 
    case 'l': 
	lflag = 1;
        length = atoi(optarg);
        printf(" legnth: %d    ", length); 
        break;	
    case 'o': 
        off1flag = 1;
        offset1 = atoi(optarg);
        printf(" offset1: %d", offset1);
        break;
    case 'O':
        off2flag = 1;
        offset2 = atoi(optarg);
        printf(" offset2: %d", offset2);
        break;
    case 'f':
        fflag = 1;
       if (optarg)
       {
       file_name = optarg;
       printf("filename: %s,", file_name);
       }
       else { 
      printf("file '%s' not found\n", optarg); 
       }

       break;
    case '?': 
        err = 1;
        printf ("Error is: %d", err);
        break;
    }



if ((fflag == 1) && (off1flag == 1) && (off2flag == 1) && (lflag == 1))
{



}
if (off2flag == 1)
    {








     }

if (off1flag ==1)
     {
     




     }

if (fflag == 1 && lflag == 1)
  {
   if (stat(file_name, &s) == 0)
   { 
     printf("Size of the file %s is %d bytes \n", file_name, (int) s.st_size);
   } 
   else
   {
     printf("You're still screwed.");
   }

    inputFd = open(file_name, O_RDONLY);     
    int outputFd; 
    
    ssize_t numRead;
    char buffer[length];
    printf("length: %d \n", length);

    numRead = read(inputFd, buffer, length);
    printf("numRead before write: %d \n", numRead);
       if (write(outputFd, buffer, length) != numRead)
         {printf("error");
        }
       else
        {
           printf("numRead %d \n", numRead);

        }
     }

  return 0;
}
